﻿//
// ItemClassificationRepository.cs
//
// Authors:
//    Lasse Andreas Hauge Petersen <09547@iha.dk>
//    Søren Flohr Nielsen <10638@iha.dk>
//    Claus Jørgensen <10229@iha.dk>
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Data.Objects;
using System.Diagnostics.Contracts;

namespace RSD.Data
{
    /// <summary>
    /// Represents a ItemClassification Repository.
    /// </summary>
    public interface IItemClassificationRepository : IRepository<ItemClassification>
    {
        /// <summary>
        /// Gets the classification by density.
        /// </summary>
        /// <param name="density">The density.</param>
        /// <returns>The matched classification; otherwise <c>null</c>.</returns>
        ItemClassification GetClassificationByDensity(double density);

        /// <summary>
        /// Gets the classification by density.
        /// </summary>
        /// <param name="density">The density.</param>
        /// <returns>The matched classification; otherwise <c>null</c>.</returns>
        ItemClassification GetPositionByDensity(double density);
    }

    /// <summary>
    /// Represents a ItemClassification Repository.
    /// </summary>
    public sealed class ItemClassificationRepository : RepositoryBase<ItemClassification>, IItemClassificationRepository
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ItemClassificationRepository"/> class.
        /// </summary>
        /// <param name="context">The object context.</param>
        public ItemClassificationRepository(IObjectContext context)
            : base(context)
        {
            Contract.Requires(context != null);
        }

        #region IItemClassificationRepository Members

        /// <summary>
        /// Gets the classification by density.
        /// </summary>
        /// <param name="density">The density.</param>
        /// <returns>
        /// The matched classification; otherwise <c>null</c>.
        /// </returns>
        public ItemClassification GetClassificationByDensity(double density)
        {
            return FindBy(item => (item.MinDensity <= density) && (density <= item.MaxDensity)).FirstOrDefault();
        }

        /// <summary>
        /// Gets the classification by density.
        /// </summary>
        /// <param name="density">The density.</param>
        /// <returns>
        /// The matched classification; otherwise <c>null</c>.
        /// </returns>
        public ItemClassification GetPositionByDensity(double density)
        {
            if (double.IsInfinity(density))
            {
                return null;
            }
                        
            return FindBy(item => (item.MinDensity <= density) && (density <= item.MaxDensity)).FirstOrDefault();
        }

        #endregion
    }
}
